package com.wmyg.tracker.dao;

import com.wmyg.tracker.message.MqAdaptPo;
import com.wmyg.tracker.sdk.FabricResult;
import com.wmyg.tracker.sdk.UltimateManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

@Slf4j
@Repository
public class FabricDao {

    @Autowired
    UltimateManager smartManager;

    public FabricResult query(MqAdaptPo po) {
        log.info("Track by args {}--------------------------------------------", Arrays.toString(po.getArgs()));
//        SmartManager smartManager = ManageBuilder.buildManagerToUse("south", "User1", "antifake", "developchannel");
        FabricResult map = smartManager.invoke(po.getMethod(), po.getArgs(), po.getIsInvoke());
        if (map.getCode() == 0) {
            log.info("Query Success");
            log.info("result is {}", map.getData());
        } else {
            switch (map.getCode()) {
                case 1:
                    log.error("The Fabric got a InvalidArgumentException cause by {}", map.getDescription());
                    break;
                case 2:
                    log.error("The Fabric got a ProposalException cause by {}", map.getDescription());
                    break;
                case 3:
                    log.error("The Fabric got a TimeoutException cause by {}", map.getDescription());
                    break;
                case 4:
                    log.error("The Fabric got a error {}", map.getData());
                    break;
                case 5:
                    log.error("The Fabric got a ExecutionException cause by {}", map.getDescription());
                    break;
                case 6:
                    log.error("The Fabric got a InterruptedException cause by {}", map.getDescription());
                    break;
                case 7:
                    log.error("The Fabric got a IOException cause by {}", map.getDescription());
                    break;
                case 8:
                    log.error("The Fabric got a TransactionException cause by {}", map.getDescription());
                    break;
                default:
                    break;
            }
        }
        return map;
    }
}
